<?php
class Products extends MY_Controller{

	function __construct()
	{
		parent::__construct();
		if(!$this->aauth->is_loggedin()){
			redirect('login');
		}
	}

	function index()
	{
		redirect('products/view/1');
	}

	function view($cat_id = null)
	{
		if(!$cat_id){
			redirect('products');
		}else{
			$product = product::find('all',array('conditions'=>array('category_id='.$cat_id),'order'=>'position asc'));
			$data['title'] = $this->title('产品管理');
			$data['list'] = $this->categorylist->getList($cat_id);
			$data['product'] = $product;
			$this->load->view($this->theme.'/header',$data);
			$this->load->view($this->theme.'/product/index');
			$this->load->view($this->theme.'/footer');
		}

	}

	function error_404()
	{
		$data['title'] = $this->title('产品');
		$this->load->view($this->theme.'/header',$data);
		$this->load->view($this->theme.'/product/error_404');
		$this->load->view($this->theme.'/footer');
	}


	function addProduct()
	{
		if($_POST){

			$this->form_validation->set_rules('cat_id','分类','required');
			$this->form_validation->set_rules('name_cn','中文名称','required');
			$this->form_validation->set_rules('name_en','荷兰语名称','required');
			$this->form_validation->set_rules('name_nl','英文名称','required');
			$this->form_validation->set_rules('reference','编号','required|is_unique[products.reference_code]');
			$this->form_validation->set_rules('price','价格','numeric');
			$this->form_validation->set_error_delimiters('<span class="text-red">', '</span>');
			if($this->form_validation->run() == false){

			}else{
				$name_cn = $_POST['name_cn'];
				$name_en = $_POST['name_en'];
				$name_nl = $_POST['name_nl'];
				$cat_id = $_POST['cat_id'];
				$active = 1;
				$positionItem = product::find('all',array('conditions'=>array('category_id'=>$cat_id)));
				$positionCount = count($positionItem);
				$position = $positionCount+1;
				$text_en = $_POST['text_en'];
				$text_nl = $_POST['text_nl'];
				$create_time = date('Y-m-d H:i:s');
				$price = $_POST['price'];
				if(!$price){
					$price = null;
				}
				$reference = $_POST['reference'];
				$newData = array(
				'category_id'=>$cat_id,
				'price'=>$price,
				'name_en'=>$name_en,
				'name_cn'=>$name_cn,
				'name_nl'=>$name_nl,
				'position'=>$position,
				'description_en'=>$text_en,
				'description_nl'=>$text_nl,
				'reference_code'=>$reference,
				'create_time'=>$create_time
				);
				$newProduct = new product($newData);
				$newProduct->save();
				if($newProduct){
					redirect('products/view/'.$newProduct->category_id);

				}else{
					$data['msg'] = array(array('code'=>3,'msg'=>'创建新的产品时遇到错误.'));
				}

			}
		}
		$data['title'] = $this->title('添加新产品');
		$data['category'] = $this->categorylist->level();
		$this->load->view($this->theme.'/header',$data);
		$this->load->view($this->theme.'/product/addProduct');
		$this->load->view($this->theme.'/footer');
	}


	function editProduct($product_id = null)
	{
		if(!is_numeric($product_id)){
			redirect('products');
		}else{
			$product = product::find_by_id($product_id);
			if(!$product){
				redirect('products/error_404');
			}else{
				if($_POST){
					$this->form_validation->set_rules('cat_id','分类','required');
					$this->form_validation->set_rules('name_cn','中文名称','required');
					$this->form_validation->set_rules('name_en','荷兰语名称','required');
					$this->form_validation->set_rules('name_nl','英文名称','required');
					$this->form_validation->set_rules('reference','编号','required|callback_check_reference');
					$this->form_validation->set_rules('price','价格','numeric');

					$this->form_validation->set_error_delimiters('<span class="text-red">', '</span>');
					if($this->form_validation->run() == false){

					}else{

						$msg = array();
						$name_cn = $_POST['name_cn'];
						$name_en = $_POST['name_en'];
						$name_nl = $_POST['name_nl'];
						$text_en = $_POST['text_en'];
						$text_nl = $_POST['text_nl'];
						$reference = $_POST['reference'];
						$price = $_POST['price'];
						if(!$price){
							$price = null;
						}
						$new_cat_id = $_POST['cat_id'];
						if($new_cat_id != $product->category_id ){
							$new = $this->changeProductCat($product_id, $new_cat_id);
							if($new){
								$msg[] = array(array('code'=>1,'msg'=>'分类修改成功'));
								redirect('products/editProduct/'.$product_id);
							}else{
								$msg[] = array(array('code'=>3,'msg'=>'分类修改不成功'));
							}

						}
						$product->name_cn = $name_cn;
						$product->name_en = $name_en;
						$product->name_nl = $name_nl;
						$product->reference_code = $reference;
						$product->price = $price;
						$product->description_en = $text_en;
						$product->description_nl = $text_nl;
						$product->updated_time = date('Y-m-d H:i:s');
						if($product->save()){
							$msg[] = array(array('code'=>1,'msg'=>'信息修改成功'));
						}else{
							$msg[] = array(array('code'=>3,'msg'=>'信息修改不成功'));
						}
						$data['msg'] = $msg;

					}
				}
				$photos = photo::find('all',array('conditions'=>array('product_id'=>$product_id),'order'=>'position asc'));
				$data['title'] = $this->title('产品 > 编辑产品');
				$data['category'] = $this->categorylist->getlistEdit($product->category_id);
				$data['product'] = product::find_by_id($product_id);
				$data['cat'] = category::find_by_id($product->category->id);
				$data['photos'] = $photos;
				$this->load->view($this->theme.'/header',$data);
				$this->load->view($this->theme.'/product/editProduct');
				$this->load->view($this->theme.'/footer');
			}
		}

	}

	function  check_reference($ref)
	{
		if(!$ref){
			redirect('products/error_404');
		}else{
			$product = product::find_by_reference_code($ref);
			if(!$product){
				return true;
			}elseif (isset($product) && $product->id == $this->uri->segment(3) ){
				return true;
			}else{
				$this->form_validation->set_message('check_reference', '编号 必须唯一。当前值已存在。');
				return false;
			}
		}
	}

	function changeProductStatus()
	{
		if(!$_POST){
			redirect('categories/error_404');
		}else{
			if($_POST['action'] == 0 ){
				$action = 1;
			}else{
				$action = 0;
			}
			$status = product::find_by_id($_POST['id']);
			$status->active = $action;
			if($status->save()){
				echo json_encode(array('result'=>true));
			}else{
				echo json_encode(array('result'=>false));
			}
		}
	}


	function changeProductPosition()
	{
		if(!$_POST){
			redirect('products/error_404');
		}else{
			$product = product::find_by_id($_POST['id']);
			$neighbor = product::find_by_position($product->position+$_POST['action']);

			$product->position = $product->position+$_POST['action'];
			$product->save();
			$neighbor->position = $neighbor->position-$_POST['action'];
			$neighbor->save();

			echo json_encode(array('result'=>true));

		}
	}


	private function changeProductCat($id, $new_cat_id)
	{

		/* new change */
		$product = product::find_by_id($id);
		$current_parent_id = $product->position;

		/* re-order current cat list  */
		$newOrder = product::find('all',array('conditions'=>array('category_id =? AND position > ?',$current_parent_id, $product->position)));
		foreach ($newOrder as $n){
			$nn = product::find_by_id($n->id);
			$nn->position = $n->position-1;
			$nn->save();
		}


		$new_neighbor = product::find('all',array('conditions'=>array('category_id'=>$new_cat_id)));
		$positionCount = count($new_neighbor);
		$position = $positionCount+1;

		$product->category_id = $new_cat_id;
		$product->position = $position;
		if($product->save()){
			return true;
		}else{
			return false;
		}



	}


	function makeCover()
	{
		if(!$_POST){
			redirect('products/error_404');
		}else{
			$photo = photo::find_by_id($_POST['photo_id']);
			$product = product::find_by_id($_POST['product_id']);
			$product->thumb = $photo->filename_s;
			$product->save();
			
			echo json_encode(array('result'=>true));

		}
	}


}